<?php
namespace app\app\controller\v1;
use app\app\model\UsersModel;
use app\app\util\BlockUtils;
use app\app\util\OnlineUtils;
use app\app\util\PointUtils;
use app\app\model\TaskModel;
use think\Db;

class TaskController extends BaseController{

    /**
     * 任务列表
     */
    public function taskList(){
        $user_id = OnlineUtils::getUserIdBySid();
        $task = new TaskModel();
        $task->user_id = $user_id;
        try{
            $data = $task->getList();
        }catch (Exception $e ){
            return jsonReturn(402,$this->error());
        }
        $user = Db::name('users')->field('answers_num')->where('id','eq',$user_id)->find();
        return jsonReturn(200,'success',['data'=>$data,'answers_num'=>$user['answers_num']]);
    }


    /**
     * 领取奖励
     */
    public function receiveReward(){
        $user_id = OnlineUtils::getUserIdBySid();
        $keyword = input('post.keyword');
        $task    = new TaskModel();
        //查询用户每日积累数据
        $data  = $task->getOne($keyword);
        if($keyword == "share"){
            //分享
            $share = BlockUtils::getShare($user_id);
            if($share >= $data['complete_num']){
                //完成
                $arr = array('user_id'=>$user_id,'task_id'=>$data['id'],'complete_num'=>$share,'create_time'=>time());
                $task->saveData($arr);
                //加积分
                $users = new UsersModel();
                $users->user_id = $user_id;
                $users->addFee($data['reword']);
                //redis积分排行
                PointUtils::getUserPoint($user_id,$data['reword']);
                return jsonReturn(200,'success',['reword'=>$data['description']]);
            }
        }elseif($keyword == "answer"){
            //答题领积分
            $answer = BlockUtils::getAnswer($user_id);
            if($answer >= $data['complete_num']){
                //完成
                $arr = array('user_id'=>$user_id,'task_id'=>$data['id'],'complete_num'=>$answer,'create_time'=>time());
                $task->saveData($arr);
                //加积分
                $users = new UsersModel();
                $users->user_id = $user_id;
                $users->addPoint($data['reword']);
                //redis积分排行
                PointUtils::getUserPoint($user_id,$data['reword']);
                return jsonReturn(200,'success',['reword'=>$data['description']]);
            }
        }else{
            //答题得红包
            $answer = BlockUtils::getAnswer($user_id);
            $user   = Db::name('users')->where('id','eq',$user_id)->find();
            if( $user['answers_num'] <5 ){
                $num    = $answer - $user['answers_num']*$data['complete_num'];
                if($num >= $data['complete_num']){
                    //完成
                    $arr = array('user_id'=>$user_id,'task_id'=>$data['id'],'complete_num'=>$num,'create_time'=>time());
                    $task->saveData($arr);
                    //加余额
                    Db::query('update a_users set fee = fee +'.$data['reword'].',answers_num = answers_num + 1 where id ='.$user_id);
                    return jsonReturn(200,'success',['reword'=>$data['description']]);
                }
            }
        }
        return jsonReturn(402,'任务完成次数不足');
    }

}